
package net.atremmedia.gwt.actionjava.examples.mouse.samples;

import net.atremmedia.gwt.actionjava.examples.AJExample;
import net.atremmedia.gwt.actionjava.examples.frame.AJExampleOverview;
import actionjava.display.Graphics;
import actionjava.display.Shape;
import actionjava.events.mouse.MouseDownHandler;
import actionjava.events.mouse.MouseEvent;
import actionjava.events.mouse.MouseEvent.MouseEventType;
import actionjava.events.mouse.MouseUpHandler;

public class MouseExample5 extends AJExample
{
	public static final String NAME = "MouseExample 5";
	public static final String DESCRIPTION = "Description";
	public static final String EXAMPLE_SRC = "https://code.google.com/p/gwt-actionjava/source/browse/trunk/aj-examples/aj-examples-mouse/src/main/java/net/atremmedia/gwt/actionjava/examples/mouse/samples/MouseExample5.java";

	private Shape testFigure;

	public MouseExample5(AJExampleOverview base)
	{
		super(NAME, DESCRIPTION, EXAMPLE_SRC, base);
	}

	@Override
	public void run()
	{
		testFigure = new Shape();
		moveFigure();
		colorFigure();

		testFigure.addEventListener(MouseEventType.MOUSE_DOWN, new MouseDownHandler()
		{
			@Override
			protected void mouseDownHandler(MouseEvent event)
			{
				mouseDown();
			}
		});

		testFigure.addEventListener(MouseEventType.MOUSE_UP, new MouseUpHandler()
		{

			@Override
			protected void mouseUpHandler(MouseEvent event)
			{
				mouseUp();
			}
		});

		addChild(testFigure);
	}

	private void mouseDown()
	{
		colorFigure();
	}

	private void mouseUp()
	{
		moveFigure();
	}

	private void moveFigure()
	{
		testFigure.setX(getRandomNumber(0, 650));
		testFigure.setY(getRandomNumber(0, 500));
	}

	private void colorFigure()
	{
		Graphics graphs = testFigure.getGraphics();
		graphs.clear();
		graphs.beginFill(getRandomColor());
		graphs.drawRect(0, 0, 150, 100);
		graphs.endFill();
	}
}
